//
//  FRLocationManager.m
//  Live
//
//  Created by 徐芙蓉 on 2017/8/22.
//  Copyright © 2017年 Afra. All rights reserved.
//

#import "FRLocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface FRLocationManager ()<CLLocationManagerDelegate>

@property(nonatomic, strong) CLLocationManager *locManager;

@property(nonatomic, copy) LoacationBlock block;

@end

@implementation FRLocationManager

+(instancetype)sharedManager {
    static FRLocationManager *_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[FRLocationManager alloc] init];
    });
    return _manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _locManager = [[CLLocationManager alloc] init];
        [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locManager.distanceFilter = 100;
        _locManager.delegate = self;
        if (![CLLocationManager locationServicesEnabled]) {
            NSLog(@"开启定位服务");
        } else {
            CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
            if (status == kCLAuthorizationStatusNotDetermined) {
                [_locManager requestWhenInUseAuthorization];
            }
        };
        
    }
    return self;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coor = location.coordinate;
    
    NSString *lat = [NSString stringWithFormat:@"%@",@(coor.latitude)];
    NSString *lon = [NSString stringWithFormat:@"%@",@(coor.longitude)];
  
    [FRLocationManager sharedManager].lat = lat;
   [FRLocationManager sharedManager].lon = lon;
    
    self.block(lat, lon);
    
    [self.locManager stopUpdatingLocation];
    
}

-(void)getGps:(LoacationBlock)block
{
    self.block = block;
    [self.locManager startUpdatingLocation];
}

@end
